﻿using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using evt;
using UnityEngine.UI;

public class SetUpView : MonoBehaviour {

	// Use this for initialization
	public static float SoundValue = 1f;
	public static float MusicValue = 1f;
	private Slider SoundSlider = null;
	private Slider MusicSlider = null;

	void Start () {
				
		EventTriggerListener.Get (gameObject).onClick = HideView;
		EventTriggerListener.Get (transform.GetChild(0).gameObject).onClick = OpenView;
		
		EventTriggerListener.Get (transform.GetChild(4).gameObject).onClick = OpenOrCloseSound;
		EventTriggerListener.Get (transform.GetChild(5).gameObject).onClick = OpenOrCloseMusic;

		EventTriggerListener.Get (transform.GetChild(6).gameObject).onClick = LoginOut;

		ServerEventDispather.getInstance ().AddEventDispather (ServerEvent.OPEN_SETUP_VIEW, OpenView);
		ServerEventDispather.getInstance ().AddEventDispather (ServerEvent.HIDE_SETUP_VIEW, HideView);
		SoundSlider = transform.GetChild (8).GetComponent<Slider> ();
		MusicSlider = transform.GetChild (7).GetComponent<Slider> ();

		tools.cashDataLoader cash = new tools.cashDataLoader ();
		string cashstr = cash.getSoundVoice();
		Debug.Log ("cashstr:-----------------------------------" + cashstr);
		if (cashstr != null) {
			MusicSlider.value = float.Parse(cashstr.Split(',')[0]);
			SoundSlider.value = float.Parse(cashstr.Split(',')[1]);
		}


	}

	private void OpenOrCloseMusic(object data){
		if (MusicSlider.value == 0f) {
			MusicSlider.value = 1f;
		} else {
			MusicSlider.value = 0f;
		}
		
	}

	private void OpenOrCloseSound(object data){
		if (SoundSlider.value == 0f) {
			SoundSlider.value = 1f;
		} else {
			SoundSlider.value = 0f;
		}
	}
	
	private void HideView(object data){
		tools.cashDataLoader cash = new tools.cashDataLoader ();
		cash.setSoundVoice (BgmPlayer.soundNum + "," + SoundPlayer.soundNum);
		transform.localPosition = new Vector3(10000,0,0);
	}

	private void OpenView(object data){
		transform.localPosition = Vector3.zero;
	}

	private void LoginOut(object data){
		//tools.cashDataLoader cash1 = new tools.cashDataLoader ();
		//cash1.setSoundVoice (BgmPlayer.soundNum + "," + SoundPlayer.soundNum);
		tools.cashDataLoader cash = new tools.cashDataLoader ();
		cash.RemoveUser ();
		Heart.StopHeart = false;
		GSocket.Closed ();
		Destroy (GameObject.Find("Main_ca"));
		Destroy (GameObject.Find("Heart"));
		Destroy (GameObject.Find("Roaring"));
		//Destroy (GameObject.Find("Reporter"));
		Destroy (GameObject.Find("BgmSinger"));
		ServerEventDispather.getInstance ().removeAll ();
		info.UserInfo.OpenID = "";
		info.UserInfo.room = null;
		SceneManager.LoadScene (0);

	}

	private void BackToLogo(){



	}
	
	// Update is called once per frame
	void Update () {
		SoundValue = SoundSlider.value;
		MusicValue = MusicSlider.value;
	}
}
